<template>
  <div class="app-container">
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-delete" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="hotelList">
      <el-table-column width="55" align="center" />
      <!-- <el-table-column label="用户ID" align="center" prop="id" /> -->
      <el-table-column label="酒店名称" align="center" prop="hotelName" />
      <el-table-column label="酒店详细地址" align="center" prop="fullAddress" />
      <el-table-column label="酒店规则" align="center" prop="rule" />
      <el-table-column label="创建日期" align="center" prop="createTime" />
      <!-- <el-table-column label="酒店剩余空房总数" align="center" prop="enabled" /> -->
      <!-- <el-table-column prop="status" label="状态" width="80">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 1">可预约</el-tag>
          <el-tag type="danger" v-else>不可预约</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleBind(scope.row)">绑定酒店规则</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除酒店规则</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="酒店规则" prop="status">
              <el-input v-model="form.rule" placeholder="请输入酒店规则" maxlength="200" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getHotelList, DelHotel,GetDetail,bindRule,updateRule } from "@/api/hotel";
import { uploadImage } from "@/api/common";
export default {
  name: "Hotel",
  data () {
    return {
      title: '',
      // 遮罩层
      loading: true,
      open: false,
      // 选中数组
      ids: [],
      form: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户数据
      hotelList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      rules: {
        rule: [
          { required: true, message: "酒店规则不能为空", trigger: "blur" }
        ],
      
       
      },
      imageUrl: "",
      hotelName:'',
      hotelId:""
    };
  },
  created () {
    this.getList();
  },
  computed: {},
  methods: {
    handleBind(row){
      this.open = true
      this.hotelName = row.hotelName
      this.hotelId = row.hotelId
      this.form.rule = row.rule
    },
    async handleUploadForm (param) {
      const formData = new FormData();
      let loading = this.$loading({
        lock: true,
        text: "上传中，请稍候...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      formData.append("file", param.file);
      try {
        const res = await uploadImage(formData);
        this.imageUrl = res.data;
        this.form.indexImg = res.data;
        loading.close();
      } catch (e) {
        loading.close();
      }
    },
    beforeAvatarUpload (file) {
      const isImage = ['image/jpeg', 'image/png'].includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isImage) {
        this.$message.error('只能上传 JPG/PNG 格式的图片!');
      }
      if (!isLt2M) {
        this.$message.error('图片大小不能超过 2MB!');
      }

      return isImage && isLt2M;
    },
    handleAdd (row) {
      this.reset();
      this.open = true;
      this.title = "添加酒店";
    },
    /** 查询酒店列表 */
    getList () {
      this.loading = true;
      this.hotelList=[]
      getHotelList(this.queryParams).then((response) => {
        this.hotelList.push(response.data)
        this.total = 1;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => {
        return item.id;
      });
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.ruleId 
      this.$modal
        .confirm("是否确认删除酒店规则？")
        .then(function () {
          return DelHotel(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      GetDetail(id).then((response) => {
        this.form.id = response.data.id
        this.form.name = response.data.hotelName
        this.form.address = response.data.fullAddress
        this.form.longitude = response.data.longitude
        this.form.latitude = response.data.latitude
        this.form.status = response.data.status==1?true:false
        this.form.indexImg = response.data.indexImg
        this.form.rule = response.data.rule
        this.imageUrl = response.data.indexImg;
        this.open = true;
        this.title = "修改酒店信息";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
            this.form.hotelName = this.hotelName
            this.form.hotelId = this.hotelId
          if (this.form.id != null) {
            updateRule(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.reset()
              this.getList();
            });
          } else {
            bindRule(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.reset()
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        rule: undefined,
      };
      this.resetForm("form");
    },
  },
};
</script>
<style scoped lang="scss">
.modal-wrap {
  ::v-deep .el-dialog__body {
    height: 600px;
    overflow-y: scroll;
  }
}

.avatar-uploader {
  ::v-deep .el-upload {
    width: 100px;
    height: 100px;
    border: 1px dotted #eee;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .avatar-uploader-icon {
    font-size: 24px;
  }
}

.banner-img {
  width: 100px;
  height: 100px;
  // margin-left: 20px;
}
.editBox{
  display: flex;
  justify-content: space-between;
}
</style>
